class CanvasRenderer {
    constructor(canvas, style) {
        this.canvas = canvas;
        this.ctx = canvas.getContext('2d');
        this.style = style;
        this.initCanvas();
    }

    initCanvas() {
        const rect = this.canvas.parentElement.getBoundingClientRect();
        this.canvas.width = rect.width * 2;
        this.canvas.height = rect.height * 2;
        this.ctx.scale(2, 2);
    }

    renderText(text) {
        this.clearCanvas();
        this.drawBackground();
        this.wrapText(text);
    }

    clearCanvas() {
        this.ctx.clearRect(0, 0, this.canvas.width, this.canvas.height);
    }

    drawBackground() {
        if(this.style.background) {
            this.ctx.fillStyle = this.style.background;
            this.ctx.fillRect(0, 0, this.canvas.width/2, this.canvas.height/2);
        }
    }

    wrapText(text) {
        const maxWidth = this.canvas.width/2 - 40;
        let fontSize = this.style.fontSize;
        let lineHeight = fontSize * this.style.lineHeight;
        let y = 30;

        // 自动缩放计算
        const words = text.split(/\s+/);
        let lines = [];
        let currentLine = words[0];
        
        // 拆分文本行逻辑...
        // [此处添加具体的文本拆分和缩放算法]

        // 应用样式
        this.ctx.font = `${fontSize}px ${this.style.fontFamily}`;
        this.ctx.fillStyle = this.style.color;
        this.ctx.textAlign = this.style.textAlign;

        // 绘制文本行
        lines.forEach(line => {
            this.ctx.fillText(line, 20, y);
            y += lineHeight;
        });
    }
}

// 样式配置
const styleConfig = {
    minimal: {
        fontFamily: 'Helvetica Neue',
        fontSize: 16,
        lineHeight: 1.6,
        color: '#333',
        textAlign: 'left',
        background: '#f8f9fa'
    },
    // 其他样式配置...
};

function generateCards() {
    const text = document.getElementById('textInput').value;
    document.querySelectorAll('.preview-canvas').forEach(canvas => {
        const style = canvas.closest('.card-style').classList[1];
        new CanvasRenderer(canvas, styleConfig[style]).renderText(text);
    });
}

// [保留原有导出功能]